<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table @row-dblclick="getClick" :data="systemTypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="数字字典名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="编号" width="120"  sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="数字字典名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <!--抽屉编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisib" :close-on-click-modal="false">
      <el-form :model="editForr" label-width="80px" :rules="editFormRule" ref="editForr">
        <el-form-item label="明细" prop="name">
          <el-input v-model="editForr.name" auto-complete="off"></el-input>
        </el-form-item>
<!--        <el-form-item label="编号" prop="types_id">-->
<!--          <el-input v-model="editForr.types_id" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisib = false">取消</el-button>
        <el-button type="primary" @click.native="edSubmit" :loading="editLoad">提交</el-button>
      </div>
    </el-dialog>

    <el-drawer
        title="我嵌套了表格!"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table @row-dblclick="getClick" :data="gridData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="明细" width="120" sortable>
        </el-table-column>

        <el-table-column label="操作" width="150">
          <template  slot="header" scope="scope">
            <el-button size="small" @click="handleA(scope.$index, scope.row)">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="handleE(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleD(scope.$index, scope.row)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="baRemove" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChang" :current-page="currentPag" :page-size="pageSiz" :total="total1" style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>
	</section>
</template>

<script>
	// import util from '../../common/js/util'
	// //import NProgress from 'nprogress'
	// import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
          keyword: ''
				},
        rowee:[],
        table:false,
        gridData:[],
        cities:[],
        title:"",
        systemTypes: [],
				total: 0,
				page: 1,
        currentPag:1,
        pageSiz:5,
        currentPage:1,
        pageSize:5,
        total1:0,
				listLoading: false,
        employeeTree:[],
				sels: [],//列表选中列

        //抽屉编辑界面是否显示
        editFormVisib: false,
        editLoad: false,
        editFormRule: {
          name: [
						{ required: true, message: '请输入明细', trigger: 'blur' }
					],
          types_id: [
						{ required: true, message: '请输入编号', trigger: 'blur' }
					],

				},
        editFormVisible: false,//编辑界面是否显示
        editLoading: false,
        editFormRules: {
          name: [
            { required: true, message: '请输入数字字典名', trigger: 'blur' }
          ],
          sn: [
            { required: true, message: '请输入编号', trigger: 'blur' }
          ],

        },
				//编辑界面数据
				editForm: {
          id:null,
          name:"",
          sn:""
				},
        //抽屉编辑界面数据
        editFor: {
          id:null,
          name:"",
          types_id:null
        },
        editForr: {
          id:null,
          name:"",
          types_id:null
        },



			}
		},
		methods: {

        getclickss(row){
        // 根据当前行数据的主键Id，发起请求获取到当前行对应的数据，进行赋值
        this.rowee = row;
        this.types_id=row.id
        let para={
          types_id :row.id,
          currentPage:this.currentPag,
          pageSize:this.pageSiz
        }
        console.log(this.currentPag);
        this.$http.post("/systemd",para)
            .then(result=>{

              this.gridData = result.data.rows;
              this.total1 = result.data.total;
            })
      },

      getClick(row){
        // 根据当前行数据的主键Id，发起请求获取到当前行对应的数据，进行赋值
        this.getclickss(row);
        this.table = true;
      },

      search(){
        this.currentPage =1
        this.getEmployees();
      },

			handleCurrentChange(val) {
				this.currentPage = val;
				this.getEmployees();
			},
      handleCurrentChang(val2){
        this.currentPag = val2;
        this.getType();
      },
			//获取用户列表
			getEmployees() {
				let para = {
          currentPage:this.currentPage,
          pageSize:this.pageSize,
          keyword: this.filters.keyword
				};
				this.listLoading = true;
        this.$http.post("/systemtype",para)

          .then(result=>{
            this.listLoading=false
            this.systemTypes = result.data.rows
            this.total = result.data.total

            console.log(this.total)
          })
        .catch(error => {
            this.$message({
              message: '系统繁忙，请稍后再试',
              type: 'success'
            });
          })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/systemtype/" + row.id)
            .then(result=>{
              console.log("1111111")
              this.listLoading=false
              if(result.data.success){
                this.$message({
                  message: result.data.message,
                  type: 'success'
                });
              }else {
                this.$message({
                  message: result.data.message,
                  type: 'error'
                });

              }
              this.currentPage=1
              this.getEmployees()
                  .catch(result=>{
                    this.listLoading=false
                    this.$message({
                      message: '系统繁忙，请重试！',
                      type: 'error'
                    });
                  })

            })
          this.$http.delete("/systemd/del/" + row.id)
              .then(result=>{
                this.listLoading=false
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });

                }
                this.currentPage=1
                this.getEmployees()
                    .catch(result=>{
                      this.listLoading=false
                      this.$message({
                        message: '系统繁忙，请重试！',
                        type: 'error'
                      });
                    })

              })

				})
			},

			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="编辑",
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
      //显示抽屉编辑界面
      handleE: function (index, row) {
        this.title="编辑",
        this.editFormVisib = true;
        this.editForr = Object.assign({}, row);
      },
			//显示新增界面
			handleAdd: function () {
        this.title="新增",
				this.editFormVisible = true;
				this.editForm = {
          id:null,
          name:"",
          sn:""
				};
			},
      //显示抽屉新增界面
      handleA: function () {
        this.title="新增",
         this.editFormVisib = true;
        this.editForr = {
          id:null,
          name:"",
          types_id:this.types_id
        };
      },
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);

              console.log("asdhuashdjashjd")
              this.$http.put("/systemtype",para)
                  .then(result=>{
                    this.editLoading = false;
                    this.editFormVisible = false
                    if(result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'error'
                      });
                    }
                    this.currentPage=1;
                    this.getEmployees()
                  })

                  .catch(result=>{
                    this.listLoading=false
                    this.$message({
                      message: '系统繁忙，请重试！',
                      type: 'error'
                    });
                  })
						});
					}
				});
			},
      //抽屉编辑
      edSubmit: function () {
        this.$refs.editForr.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoad = true;
              //NProgress.start();
              let para = Object.assign({}, this.editForr);


              this.$http.put("/systemd",para)
                  .then(result=>{
                    this.editLoad = false;
                    this.editFormVisib = false
                    if(result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'error'
                      });
                    }
                    this.currentPag=1;
                    this.getType();
                  })

                  .catch(result=>{
                    this.listLoading=false
                    this.$message({
                      message: '系统繁忙，请重试！',
                      type: 'error'
                    });
                  })
            });
          }
        });
      },
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          this.listLoading = true;
          //NProgress.start();
          this.$http.patch("/systemtype/",ids)
              .then(result=>{
                this.listLoading=false
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
              })
          this.currentPage=1
          this.getEmployees()
              .catch(result=>{
                this.listLoading=false
                this.$message({
                  message: '系统繁忙，请重试！',
                  type: 'error'
                });
              })
				}).catch(() => {

				});
			},
      //抽屉批量删除
      baRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          this.$http.patch("/systemd/",ids)
              .then(result=>{
                this.listLoading=false
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
              })
              this.currentPag=1
              this.getType()
              .catch(result=>{
                this.listLoading=false
                this.$message({
                  message: '系统繁忙，请重试！',
                  type: 'error'
                });
              })
        }).catch(() => {

        });
      },
      getEmployeeTree(){
        this.$http.get("/dept")
            .then(result=>{
              this.cities = result.data
            })
      },
      getType(){
        // let para = {
        //   currentPag:this.currentPag,
        //   pageSiz:this.pageSiz,
        //   keyword: this.filters.keyword,
        //   types_id:this.types_id
        // };
        // console.log("对滴" + this.currentPag)
        // this.$http.post("/systemd",para)
        //     .then(result=>{
        //       this.gridData = result.data.rows;
        //       this.total1 = result.data.total;
        //     })
        this.getclickss(this.rowee);
      },
      getTypee(){
        // let para = {
        //   currentPag:this.currentPag,
        //   pageSiz:this.pageSiz,
        //   keyword: this.filters.keyword,
        //   types_id:this.types_id
        // };
        // console.log("对滴" + this.currentPag)
        // this.$http.post("/systemd",para)
        //     .then(result=>{
        //       this.gridData = result.data.rows;
        //       this.total1 = result.data.total;
        //     })
        this.getclickss(this.rowee);
      },
      //抽屉删除
      handleD: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.delete("/systemd/" + row.id)
              .then(result=>{
                this.listLoading=false
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success',
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
              })
            this.currentPag=1
            this.getTypee()
              .catch(result=>{
            this.listLoading=false
            this.$message({
              message: '系统繁忙，请重试！',
              type: 'error'
            });
          })

        })
      },

		},
		mounted() {
			this.getEmployees();
      this.getEmployeeTree();
      this. getType();
		}
	}

</script>

<style scoped>

</style>